package test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class test extends JFrame {
    private JTextField nameField;
    private JComboBox<String> majorComboBox;
    private JComboBox<String> gradeComboBox;
    private JTextArea introductionArea;
    private JButton chineseButton;
    private JButton englishButton;

    public test() {

        setTitle("姓名(学号)");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setLayout(new BorderLayout());

        JPanel inputPanel = new JPanel();
        inputPanel.setLayout(new GridLayout(3, 2));

        nameField = new JTextField("请输入姓名");
        inputPanel.add(new JLabel("姓名:"));
        inputPanel.add(nameField);

        String[] majors = {"计算应", "计应企"};
        majorComboBox = new JComboBox<>(majors);
        inputPanel.add(new JLabel("专业:"));
        inputPanel.add(majorComboBox);
        String[] grades = {"2021", "2022"};

        gradeComboBox = new JComboBox<>(grades);
        inputPanel.add(new JLabel("年级:"));
        gradeComboBox.setEditable(true);
        inputPanel.add(gradeComboBox);

        add(inputPanel, BorderLayout.NORTH);
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout());

        chineseButton = new JButton("中文");
        buttonPanel.add(chineseButton);

        englishButton = new JButton("英文");
        buttonPanel.add(englishButton);

        add(buttonPanel, BorderLayout.CENTER);
        introductionArea = new JTextArea("我的名字是XX");
        introductionArea.setEditable(false);

        add(new JScrollPane(introductionArea), BorderLayout.SOUTH);
        chineseButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name = nameField.getText();
                introductionArea.setText("我的名字是" + name);
            }
        });

        englishButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name = nameField.getText();
                introductionArea.setText("My name is " + name);
            }
        });
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new test();
            }
        });
    }
}
